#include "maprecordwidget.h"
#include "ui_maprecordwidget.h"

MapRecordWidget::MapRecordWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MapRecordWidget)
{
    ui->setupUi(this);
    mapWidget = new MapWidget(ui->mainStack);
    ui->mainStack->addWidget(mapWidget);
    ui->start->setEnabled(false);

    mapCtrl = new MapCtrl(this);
    connect(mapCtrl, SIGNAL(carState(int,int,int)), this, SLOT(recvCarStateInfo(int,int,int)));
//    connect(mapWidget, SIGNAL(gotoPos(QPointF,int)), mapCtrl, SLOT(gotoPosReq(QPointF,int)));//前往目标点
//    connect(mapCtrl, SIGNAL(carPos(QPointF, int)), mapWidget, SLOT(recvCarPos(QPointF,int)));//更新汽车坐标
//    connect(mapCtrl, SIGNAL(carState(bool)), this, SLOT(carStateChanged(bool)));//汽车状态改变

    winSettings = new SettingWidget();
    connect(winSettings, SIGNAL(updateMap()), mapWidget, SLOT(updatePath()));//更新地图
    connect(winSettings, SIGNAL(updateStation()), mapWidget, SLOT(updateStation()));//更新站点

    gRecorder = new GpsRecorder();
    connect(gRecorder, SIGNAL(readyRecorder()), this, SLOT(recorderReady()));
    connect(gRecorder, SIGNAL(newMapPoint(double,double,QString,QString,QString,QString,int)), mapWidget, SLOT(newPos(double,double,QString,QString,QString,QString,int)));

    gReader = new GpsReader();
    connect(gReader, SIGNAL(rtkState(bool)), this, SLOT(rtkStateChanged(bool)));
    connect(gReader, SIGNAL(rtkState(bool)), ui->start, SLOT(setEnabled(bool)));
//    gReader->testRtkState(true);
}

MapRecordWidget::~MapRecordWidget()
{
    delete ui;
    winSettings->deleteLater();
}

void MapRecordWidget::carStateChanged(bool ismove)
{
    ui->start->setEnabled(!ismove);
    //    ui->stop->setEnabled(ismove);
}

void MapRecordWidget::rtkStateChanged(bool checked)
{
    if(checked)
    {
        ui->rtk_state->setText("GPS已就绪");
        ui->rtk_state->setStyleSheet("background-color: rgb(115, 210, 22);color: rgb(238, 238, 236);");
    }
    else
    {
        ui->rtk_state->setText("GPS未就绪");
        ui->rtk_state->setStyleSheet("background-color: rgb(239, 41, 41);color: rgb(238, 238, 236);");
    }
}

void MapRecordWidget::recvCarStateInfo(int speed, int acce, int brake)
{
    ui->speed->display(speed);
    ui->acce->setValue(acce);
    ui->brake->setValue(brake);
}

void MapRecordWidget::recorderReady()
{
    ui->start->setChecked(true);
    gReader->recordStart();
    gRecorder->recordStart();
    connect(gReader, SIGNAL(newPos(double,double,QString,QString,QString,QString)), gRecorder, SLOT(saveNewPos(double,double,QString,QString,QString,QString)));
}

void MapRecordWidget::on_start_clicked()
{
//    mapCtrl->carCtrlStart();
}

//void MainWidget::on_stop_clicked()
//{
//    mapCtrl->carCtrlStop();
//}

void MapRecordWidget::on_settings_clicked()
{
    gRecorder->showMaximized();
//    winSettings->showFullScreen();
}

void MapRecordWidget::on_view_clicked(bool checked)
{
    mapWidget->setGlobalView(!checked);
    if(!checked)
    {
        ui->view->setText("放大");
    }
    else
    {
        ui->view->setText("地图");
    }
}

void MapRecordWidget::on_makr_light_clicked(bool checked)
{
    if(!checked)
    {
        gRecorder->setPosMark(0);
    }
    else
    {
        gRecorder->setPosMark(1);
//        ui->makr_light->setChecked(false);
        ui->mark_curve->setChecked(false);
        ui->mark_sidewalk->setChecked(false);
        ui->mark_station->setChecked(false);
    }

}

void MapRecordWidget::on_mark_sidewalk_clicked(bool checked)
{
    if(!checked)
    {
        gRecorder->setPosMark(0);
    }
    else
    {
        gRecorder->setPosMark(2);
        ui->makr_light->setChecked(false);
        ui->mark_curve->setChecked(false);
//        ui->mark_sidewalk->setChecked(false);
        ui->mark_station->setChecked(false);
    }
}

void MapRecordWidget::on_mark_curve_clicked(bool checked)
{
    if(!checked)
    {
        gRecorder->setPosMark(0);
    }
    else
    {
        gRecorder->setPosMark(3);
        ui->makr_light->setChecked(false);
//        ui->mark_curve->setChecked(false);
        ui->mark_sidewalk->setChecked(false);
        ui->mark_station->setChecked(false);
    }
}

void MapRecordWidget::on_mark_station_clicked(bool checked)
{
    if(!checked)
    {
        gRecorder->setPosMark(0);
    }
    else
    {
        gRecorder->setPosMark(4);
        ui->makr_light->setChecked(false);
        ui->mark_curve->setChecked(false);
        ui->mark_sidewalk->setChecked(false);
//        ui->mark_station->setChecked(false);
    }
}

void MapRecordWidget::on_start_toggled(bool checked)
{
    if(checked)
    {
        ui->start->setText("停止");
    }
    else
    {
        ui->start->setText("开始");
    }
}

void MapRecordWidget::on_start_clicked(bool checked)
{
    if(checked)
    {
        ui->start->setChecked(false);
        gRecorder->showMaximized();
    }
    else
    {
        disconnect(gReader, SIGNAL(newPos(double,double,QString,QString,QString,QString)), gRecorder, SLOT(saveNewPos(double,double,QString,QString,QString,QString)));
        gRecorder->saveMap();
    }
}

void MapRecordWidget::on_flag_1_clicked(bool checked)
{
    if(checked)
        gRecorder->setPosFlag(1);
}

void MapRecordWidget::on_flag_2_clicked(bool checked)
{
    if(checked)
        gRecorder->setPosFlag(2);
}

void MapRecordWidget::on_flag_3_clicked(bool checked)
{
    if(checked)
        gRecorder->setPosFlag(3);
}

void MapRecordWidget::on_flag_4_clicked(bool checked)
{
    if(checked)
        gRecorder->setPosFlag(4);
}
